package com.hl.card.modular.packBack.controller;

import com.hl.card.common.controller.BaseController;
import com.hl.card.common.persistence.model.ProfitScale;
import com.hl.card.core.log.LogObjectHolder;
import com.hl.card.core.support.BeanKit;
import com.hl.card.modular.packBack.service.GoodsSevice;
import com.hl.card.modular.packBack.service.IProfitScaleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huangpu
 * @since 2018-06-05
 */
@Controller
@RequestMapping("/profitScale")
public class ProfitScaleController extends BaseController {

    @Resource
    private IProfitScaleService iProfitScaleService;

    @Resource
    private GoodsSevice goodsSevice;


    private String PREFIX = "/packback/profitScale/";


    @RequestMapping("")
    public String index(){
        return PREFIX+"profitScale.html";
    }


    /**
     * 跳转到添加
     */
    @RequestMapping(value = "/profitScale_add")
    public Object profitScaleAdd(Model model){
        List<Map<String, Object>> list= goodsSevice.selectList();
        model.addAttribute("goods",list);
        LogObjectHolder.me().set(list);
        return PREFIX+"profitScale_add.html";
    }


    /**
     * 跳转到修改销售利润管理
     */
    @RequestMapping(value = "/profitScale_update/{profitScaleId}")
    public Object profitScaleUpdate(@PathVariable Integer profitScaleId, Model model){
        List<Map<String, Object>> list= goodsSevice.selectList();
        ProfitScale profitScale = iProfitScaleService.selectById(profitScaleId);
        Map<String, Object> profitScalemap = BeanKit.beanToMap(profitScale);
        model.addAttribute("profitScale",profitScalemap);
        model.addAttribute("goods",list);
        LogObjectHolder.me().set(list);
        LogObjectHolder.me().set(profitScale);

        return PREFIX+"profitScale_edit.html";
    }


    /**
     * 获取销售利润列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> list= iProfitScaleService.selectList();
        System.out.println(list);
        return list;
    }
    /**
     * 删除
     */
    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @ResponseBody
    public Object del(Integer id){
        iProfitScaleService.delete(id);
        return super.SUCCESS_TIP;
    }
    /**
     * 添加
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(ProfitScale profitScale){
        ProfitScale profitScale1 = iProfitScaleService.selectByName(profitScale.getGoodsCode());
        if(profitScale1 == null) {
            Double member = profitScale.getMember();
            Double partner = profitScale.getPartenr();
            Double bibi = profitScale.getBibi();
            if (member + partner + bibi <= 100) {
                iProfitScaleService.save(profitScale);
                return super.SUCCESS_TIP;
            } else {
                return super.FailuerTip("会员和合伙人还有比比旅行的总额不能大于100");

            }
        }else{
            return super.FailuerTip("改产品类型已存在");
        }
    }
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(@Valid ProfitScale profitScale){
        Double member = profitScale.getMember();
        Double partner = profitScale.getPartenr();
        Double bibi = profitScale.getBibi();
        if(member + partner + bibi <= 100) {
            iProfitScaleService.updateById(profitScale);
            return super.SUCCESS_TIP;
        }else{
            return super.FailuerTip("会员和合伙人还有比比旅行的总额不能大于100");

        }

    }

}
